<?php
namespace app\index\controller;

class Index extends BaseController
{
    /**
     * 前端首页
     * @return string
     * @throws \think\Exception
     */
    public function index()
    {
//        return \think\Response::create(\think\Url::build('/admin'), 'redirect');
//        新闻
        $map=[
            'status'=>1,
            'isdelete'=>0
        ];
        $newsList= model("News")->where($map)->select();
        $this->assign("newsList",$newsList);
//        项目
        $kindList= model("Kind")->where($map)->select();
        $this->assign("kindList",$kindList);
        $this->assign("kindList2",$kindList);
        $this->assign("kindList3",$kindList);
//        班级
        $stuList=model("Stu")->where($map)->select();
        $this->assign("stuList",$stuList);
        $this->assign("stuList2",$stuList);
//        个人成绩

        return $this->view->fetch();
    }
    /**
     * 新闻详情
     */
    public function newsDetail()
    {
        $id = $this->request->param('id');
        if (!isset($id)) {
            return ajax_return_adv_error('缺少参数');
        }
        $map=[
            'status'=>1,
            'isdelete'=>0,
            'id'=>$id
        ];
        $newsDetail= model("News")->where($map)->find();
        $this->assign("newsDetail",$newsDetail);
        return $this->view->fetch();
    }
    /**
     * 报名保存
     */
    public function userSave()
    {
        $data=$this->request->param();
        if(!isset($data)){
            $this->error("缺少参数！");
//            return ajax_return_adv_error("缺少参数！");
        }
//        查找是否已经报过名，防止重复报名
        $map=[
            "user_name"=>$data['user_name'],
            "user_kind"=>$data['user_kind'],
            "user_class"=>$data['user_class']
        ];
        $name=model("User")->where($map)->find();
        if($name){
            $this->error("您已报名该项目，请勿重复报名！");
        }
//      通过过滤字段和选择非修改
        $res=model("User")->allowField(true)->isUpdate(false)->save($data);
        if($res){

            $this->success("报名成功！");
        }
        else{
           $this->error("系统出错！");
        }

    }
}
